#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QPushButton>
#include <QGridLayout>
#include <QGroupBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , tcpSocket(new QTcpSocket(this))
{
    ui->setupUi(this);
    
    // 创建控制面板
    controlPanel = new QWidget(this);
    controlPanel->setGeometry(0, 100, this->width(), this->height()-100);
    controlPanel->hide();
    
    // 控制面板布局
    QVBoxLayout *mainLayout = new QVBoxLayout(controlPanel);
    
    // LED控制组
    QGroupBox *ledGroup = new QGroupBox("LED控制", controlPanel);
    QGridLayout *ledLayout = new QGridLayout();
    
    QPushButton *ledButtons[] = {
        new QPushButton("LED2开"), new QPushButton("LED2关"),
        new QPushButton("LED3开"), new QPushButton("LED3关"),
        new QPushButton("LED4开"), new QPushButton("LED4关"),
        new QPushButton("LED5开"), new QPushButton("LED5关")
    };
    
    for(int i = 0; i < 8; i++) {
        ledLayout->addWidget(ledButtons[i], i/2, i%2);
    }
    ledGroup->setLayout(ledLayout);
    mainLayout->addWidget(ledGroup);

    // 音符控制组
    QGroupBox *musicGroup = new QGroupBox("音符控制", controlPanel);
    QGridLayout *musicLayout = new QGridLayout();
    
    QPushButton *musicButtons[] = {
        new QPushButton("Do开"), new QPushButton("Do关"),
        new QPushButton("Re开"), new QPushButton("Re关"),
        new QPushButton("Mi开"), new QPushButton("Mi关"),
        new QPushButton("Fa开"), new QPushButton("Fa关"),
        new QPushButton("Sol开"), new QPushButton("Sol关"),
        new QPushButton("La开"), new QPushButton("La关"),
        new QPushButton("Si开"), new QPushButton("Si关")
    };
    
    for(int i = 0; i < 14; i++) {
        musicLayout->addWidget(musicButtons[i], i/4, i%4);
    }
    musicGroup->setLayout(musicLayout);
    mainLayout->addWidget(musicGroup);

    // 连接按钮信号
    connect(ledButtons[0], &QPushButton::clicked, this, [this]() { tcpSocket->write("1"); });
    connect(ledButtons[1], &QPushButton::clicked, this, [this]() { tcpSocket->write("2"); });
    connect(ledButtons[2], &QPushButton::clicked, this, [this]() { tcpSocket->write("3"); });
    connect(ledButtons[3], &QPushButton::clicked, this, [this]() { tcpSocket->write("4"); });
    connect(ledButtons[4], &QPushButton::clicked, this, [this]() { tcpSocket->write("5"); });
    connect(ledButtons[5], &QPushButton::clicked, this, [this]() { tcpSocket->write("6"); });
    connect(ledButtons[6], &QPushButton::clicked, this, [this]() { tcpSocket->write("7"); });
    connect(ledButtons[7], &QPushButton::clicked, this, [this]() { tcpSocket->write("8"); });

    connect(musicButtons[0], &QPushButton::clicked, this, [this]() { tcpSocket->write("q"); });
    connect(musicButtons[1], &QPushButton::clicked, this, [this]() { tcpSocket->write("w"); });
    connect(musicButtons[2], &QPushButton::clicked, this, [this]() { tcpSocket->write("e"); });
    connect(musicButtons[3], &QPushButton::clicked, this, [this]() { tcpSocket->write("r"); });
    connect(musicButtons[4], &QPushButton::clicked, this, [this]() { tcpSocket->write("t"); });
    connect(musicButtons[5], &QPushButton::clicked, this, [this]() { tcpSocket->write("y"); });
    connect(musicButtons[6], &QPushButton::clicked, this, [this]() { tcpSocket->write("u"); });
    connect(musicButtons[7], &QPushButton::clicked, this, [this]() { tcpSocket->write("i"); });
    connect(musicButtons[8], &QPushButton::clicked, this, [this]() { tcpSocket->write("o"); });
    connect(musicButtons[9], &QPushButton::clicked, this, [this]() { tcpSocket->write("p"); });
    connect(musicButtons[10], &QPushButton::clicked, this, [this]() { tcpSocket->write("a"); });
    connect(musicButtons[11], &QPushButton::clicked, this, [this]() { tcpSocket->write("s"); });
    connect(musicButtons[12], &QPushButton::clicked, this, [this]() { tcpSocket->write("d"); });
    connect(musicButtons[13], &QPushButton::clicked, this, [this]() { tcpSocket->write("f"); });

    // 连接成功时显示控制面板
    connect(tcpSocket, &QTcpSocket::connected, this, [this]() {
        ui->pushButton_connect->setEnabled(false);
        ui->lineEdit_ip->setEnabled(false);
        ui->lineEdit_port->setEnabled(false);
        ui->pushButton_disconnect->show();  // 显示断开按钮
        controlPanel->show();
    });

    // 错误处理
    connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, [this](QAbstractSocket::SocketError) {
        QMessageBox::critical(this, "连接错误", "无法连接到服务器: " + tcpSocket->errorString());
    });
}

Widget::~Widget()
{
    delete ui;
    delete tcpSocket;
}

void Widget::on_pushButton_connect_clicked()
{
    QString ip = ui->lineEdit_ip->text();
    QString portStr = ui->lineEdit_port->text();
    bool ok;
    quint16 port = portStr.toUShort(&ok);
    
    if (!ok || port == 0) {
        QMessageBox::warning(this, "输入错误", "请输入有效的端口号");
        return;
    }
    
    tcpSocket->connectToHost(ip, port);
}

void Widget::on_pushButton_disconnect_clicked()
{
    // 断开连接
    tcpSocket->disconnectFromHost();
    
    // 隐藏控制面板
    controlPanel->hide();
    
    // 恢复连接界面状态
    ui->pushButton_connect->setEnabled(true);
    ui->lineEdit_ip->setEnabled(true);
    ui->lineEdit_port->setEnabled(true);
    
    // 初始状态隐藏断开按钮
    ui->pushButton_disconnect->hide();
}